import pathToRegexp from 'path-to-regexp/index';
import {getPoint,postPoint,putPoint,deletePoint,getMaillist,getCheck } from 'services/app';
export default {
    namespace: 'point',
    state: {
        pages:1,
        record:{},
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //获取巡检路线
        *getPoint ({ payload }, { call, put }){
            const {data ,total} = yield call(getPoint, payload);
            localStorage.removeItem('hrefid');
            yield put({ type: 'save', payload: {getPointData:data,total:total,pages:payload.pageNumber}});
        },
        //获取添加数据
        *newPost ({ payload }, { call, put }){
            const data = yield call(getMaillist, {
                name:"",
                phone :"",
                pageNumber :0,
                pageSize :0,
            });
            const dataTow = yield call(getCheck, {
                name:"",
                location :"",
                num:'',
                route_id:"",
                pageNumber :0,
                pageSize :0,
            });
            yield put({ type: 'save', payload: {FZR:data.data,XJD:dataTow.data,view:true}});
        },
        //添加巡检路线
        *postPoint ({ payload }, { call, put }){
            const data = yield call(postPoint, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {view:false,pages:0}});
            }
        },
        //修改巡检路线
        *putPoint ({ payload }, { call, put }){
            const data = yield call(putPoint, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {view:false,pages:0}});
            }
        },
        //删除巡检路线
        *deletePoint ({ payload }, { call, put }){
            const data = yield call(deletePoint, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {view:false,pages:0}});
            }
        },
        //输入等于输出
        *inputOut ({ payload }, { put }){
            yield put({ type: 'save', payload: payload });
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
